#include <stdio.h>
#include <stdarg.h>
#include "communication/message.h"
#include "utils/logger.h"
#include "utils/utils.h"

static char log_cache[128];
static void log_fputc(char c);

static void log_out(char *fmt, va_list args){
	int len = vsnprintf(log_cache, sizeof(log_cache), fmt, args);
	for (int i = 0; i < len; i++) {
		log_fputc(log_cache[i]);
	}
}

void log_debug(int mod, char *fmt, ...){
	va_list args;
	va_start(args, fmt);		
	log_out(fmt, args);
	va_end(args);
}

static u8 log_buffer[64];
static int log_index = 0;
int can_fputc(char c){
	if (log_index < sizeof(log_buffer)) {
		log_buffer[log_index++] = c;
	}
	if ((c == '\n') || log_index == sizeof(log_buffer)){
		protocol_send_indicator(CMD_Log, log_buffer, log_index);
		log_index = 0;
	}
}
